#!/bin/bash
set -ex

cd $(dirname $0)/..

. ./scripts/version

mkdir -p bin

if [ "$CROSS" = 1 ]; then
    CGO_ENABLED=0 GOOS=darwin go build -ldflags "-X main.VERSION=$VERSION"  -o ./bin/rancher-machine-Darwin-x86_64 ./cmd/rancher-machine
    CGO_ENABLED=0 GOOS=windows go build -ldflags "-X main.VERSION=$VERSION" -o ./bin/rancher-machine-Windows-x86_64.exe ./cmd/rancher-machine
    CGO_ENABLED=0 GOARCH=arm64 go build -a -tags netgo -installsuffix netgo -ldflags "-X main.VERSION=$VERSION" -o ./bin/rancher-machine-Linux-arm64 ./cmd/rancher-machine
    CGO_ENABLED=0 GOARCH=ppc64le go build -a -tags netgo -installsuffix netgo -ldflags "-X main.VERSION=$VERSION" -o ./bin/rancher-machine-Linux-ppc64le ./cmd/rancher-machine
    CGO_ENABLED=0 GOARCH=s390x go build -a -tags netgo -installsuffix netgo -ldflags "-X main.VERSION=$VERSION" -o ./bin/rancher-machine-Linux-s390x ./cmd/rancher-machine
    CGO_ENABLED=0 GOARCH=arm GOARM=6 go build -a -tags netgo -installsuffix netgo -ldflags "-X main.VERSION=$VERSION" -o ./bin/rancher-machine-Linux-arm ./cmd/rancher-machine
    CGO_ENABLED=0 GOARCH=arm GOARM=6 go build -a -tags netgo -installsuffix netgo -ldflags "-X main.VERSION=$VERSION" -o ./bin/rancher-machine-Linux-armv6l ./cmd/rancher-machine
    CGO_ENABLED=0 GOARCH=arm GOARM=7 go build -a -tags netgo -installsuffix netgo -ldflags "-X main.VERSION=$VERSION" -o ./bin/rancher-machine-Linux-armv7l ./cmd/rancher-machine
    CGO_ENABLED=0 GOARCH=amd64 go build -a -tags netgo -installsuffix netgo -ldflags "-X main.VERSION=$VERSION -extldflags '-static -s'" -o ./bin/rancher-machine-Linux-x86_64 ./cmd/rancher-machine
    cp ./bin/rancher-machine-Linux-arm64 ./bin/rancher-machine-Linux-aarch64

    cp ./bin/rancher-machine-$(uname -s)-$(uname -m) ./bin/rancher-machine
    echo Built ./bin/rancher-machine-$(uname -s)-$(uname -m)
else
    CGO_ENABLED=0 go build -a -tags netgo -installsuffix netgo \
        -ldflags "-w -s -extldflags '-static'
         -X github.com/rancher/machine/version.Version=$VERSION
         -X github.com/rancher/machine/version.GitCommit=$COMMIT" \
        -o ./bin/rancher-machine ./cmd/rancher-machine
    ./bin/rancher-machine --version
fi
